import sys, os, re, ConfigParser
from sets import Set

if not os.path.exists(os.path.join(os.path.dirname(__file__), 'config.ini')):
    configDir = '/etc/ts2mkv/'
    projectXDir = '/usr/share/ts2mkv/'
else:
    configDir = os.path.split(__file__)[0]
    projectXDir = os.path.split(__file__)[0]

config = ConfigParser.ConfigParser()
config.read(os.path.join(configDir, 'config.ini'))

settings = dict(config.items("settings"))
settings['projectXDir'] = projectXDir
settings['configDir'] = configDir

videoPresetItems = config.items('videopresets')
audioPresetItems = config.items('audiopresets')
allPresetItems = videoPresetItems + audioPresetItems

pidTeletextPageMap = dict(config.items('teletextpages'))

toolPaths = dict(config.items('tools'))

def _getAvailablePresets(items):
    return [(k.split('.')[0])
        for k, v in items
        if k.split('.')[1] == 'name']

def _getPresetData(items, preset):
    return dict([(k.split('.')[1], v)
        for k, v in items
        if k.split('.')[0] == preset])

def getAvailableVideoPresets():
    return _getAvailablePresets(videoPresetItems)

def getAvailableAudioPresets():
    return _getAvailablePresets(audioPresetItems)

def getPresetData(preset):
    return _getPresetData(allPresetItems, preset)

if __name__ == '__main__':
    print
    print

    currentVideoPreset = settings['videopreset']
    print 'Current video preset:', currentVideoPreset
    data = getPresetData(currentVideoPreset)
    for d in data:
        print '  %s: %s' % (d, data[d])

    print
    print
    currentAudioPreset = settings['audiopreset']
    print 'Current audio preset:', currentAudioPreset
    data = getPresetData(currentAudioPreset)
    for d in data:
        print '  %s: %s' % (d, data[d])

    print
    print
    print 'PID/TeletextPage map:'
    print pidTeletextPageMap

    print
    print
    print 'All settings:'
    for setting in settings:
        print '  %s: %s' % (setting, settings[setting])
    print
